package com.shop2cn.iapi.grouponmgr.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.grouponmgr.parameter.SaveGrouponReq;
import com.shop2cn.iapi.grouponmgr.parameter.StopGrouponReq;
import com.shop2cn.iapi.grouponmgr.service.StopGrouponCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.business.service.Shop2cnGrouponCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
/**
*停止拼团
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_StopGroupon{
	private static StopGrouponReq stopgrouponBean;
	private static StopGrouponCall stopgrouponCall;
	YmtProductsIWapper YmtProductsIWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("停止拼团");
	}

	@Before
	public void caseUp() {
		stopgrouponBean=new StopGrouponReq();
		stopgrouponCall=new StopGrouponCall();
		YmtProductsIWapper=new YmtProductsIWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(stopgrouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyMongoCurMq(Integer getGrouponProductId, String pid, String code) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(1000);
		List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null, 1);
		String version = gp.get(0).getVersion().toString();
		MessageListBean MessageListBean2 = MqQueryCallService.initPMongoCrud(pid);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		Logger.debug("joa2:"+joa2);
		Logger.verifyEquals(code, joa2.get(0).get("ActionType"), "验证ActionType");
		Logger.verifyEquals(getGrouponProductId, joa2.get(0).get("ActivityId"), "验证ActivityId");
	}

	private void verifyGroupMq(Integer getGrouponProductId, String pid,String qcode) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(1000);
		MessageListBean MessageListBean = MqQueryCallService.initGroupon(pid, qcode);
		List<Map> joa = MqQueryCallService.GetMqMessage(MessageListBean);
		Logger.debug(joa);
		// {"EndTime":"4000-01-31
		// 23:59:00","GroupExpiredHours":6,"GroupMemberCount":2,"GrouponProductId":33,"ProductId":"p1158472"}
		Logger.comment("验证砍价团总线消息");
		Logger.verifyEquals(getGrouponProductId, joa.get(0).get("GrouponProductId"), "验证GrouponProductId");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_StopGroupon_001() {
		Logger.start(true,"停止团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			stopgrouponBean.setGrouponProductId(grouponProductId);
			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("200", stopgrouponCall.getString("code"), "验证code");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null, 1);
			//状态：0- 正常，1- 终止拼团，-1- 删除拼团，2- 异常拼团
			Logger.verifyEquals(1, gp.get(0).getStatus(),"验证停止拼团");
			verifyMongoCurMq(gp.get(0).getGrouponProductId(),pid,"StopGroupon");
			verifyGroupMq(gp.get(0).getGrouponProductId(),pid,"stop_groupon_god");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_StopGroupon_002() {
		Logger.start(true,"不输入sellerId失败");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			stopgrouponBean.setGrouponProductId(grouponProductId);
//			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("400", stopgrouponCall.getString("code"), "验证code");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null,0);
			//状态：0- 正常，1- 终止拼团，-1- 删除拼团，2- 异常拼团
			Logger.verifyEquals(0, gp.get(0).getStatus(),"验证停止拼团");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_StopGroupon_003() {
		Logger.start(true,"不输入grouponProductId失败");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
//			stopgrouponBean.setGrouponProductId(grouponProductId);
			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("400", stopgrouponCall.getString("code"), "验证code");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null,0);
			//状态：0- 正常，1- 终止拼团，-1- 删除拼团，2- 异常拼团
			Logger.verifyEquals(0, gp.get(0).getStatus(),"验证停止拼团");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_StopGroupon_004() {
		Logger.start(true,"grouponProductId和sellerid不匹配失败");
		try {

			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			stopgrouponBean.setGrouponProductId(grouponProductId+1);
			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("201", stopgrouponCall.getString("code"), "验证code");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null, 0);
			//状态：0- 正常，1- 终止拼团，-1- 删除拼团，2- 异常拼团
			Logger.verifyEquals(0, gp.get(0).getStatus(),"验证停止拼团");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_StopGroupon_005() {
		Logger.start(true,"一个商品反复开团停团成功");
		try {
			Integer mchId = GodmchManageCallService.CreateSellerMch();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(mchId,"AutoTest",false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			List<String> catalogList =new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(mchId,pid);
			selectProductDetails.getCatalogList().forEach(a->{
		 		catalogList.add(a.getCatalogId());
		 	});
			
			SaveGrouponReq savegrouponBean = Shop2cnGrouponCallService.initSaveGrouponReq(mchId, pid, catalogList.get(0), 100d);
			Integer grouponProductId = Shop2cnGrouponCallService.SaveGrouponCall(savegrouponBean);
			stopgrouponBean.setGrouponProductId(grouponProductId);
			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("200", stopgrouponCall.getString("code"), "验证code");
			stopgrouponBean.setGrouponProductId(grouponProductId);
			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("200", stopgrouponCall.getString("code"), "验证code");
			stopgrouponBean.setGrouponProductId(grouponProductId);
			stopgrouponBean.setSellerId(mchId);
			stopgrouponCall.setData(stopgrouponBean);
			stopgrouponCall.callService();			
			Logger.verifyEquals("200", stopgrouponCall.getString("code"), "验证code");
			List<GrouponProducts> gp = YmtProductsIWapper.selectGrouponProducts(pid, null, 1);
			//状态：0- 正常，1- 终止拼团，-1- 删除拼团，2- 异常拼团
			Logger.verifyEquals(1, gp.get(0).getStatus(),"验证停止拼团");
			verifyMongoCurMq(gp.get(0).getGrouponProductId(),pid,"StopGroupon");
			verifyGroupMq(gp.get(0).getGrouponProductId(),pid,"stop_groupon_god");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}